<div class="layui-card">
    <div class="layui-card-header">
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>资源管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="resource-search-key">
                <option value="sysId">平台</option>
            </select>&emsp;
            <select id="resource-search-value">
                <option value="">-请选择-</option>
                <option value="1">安卓</option>
                <option value="2">苹果</option>
            </select>&emsp;
            <button id="resource-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="resource-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="resource-table" lay-filter="resource-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="resource-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="resource-model">
    <form id="resource-form" lay-filter="resource-form" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">版本</label>
            <div class="layui-input-block">
                <input name="version" placeholder="请输入版本" type="text" class="layui-input" maxlength="20"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">下载地址</label>
            <div class="layui-input-block">
                <input name="downUrl" placeholder="请输入下载地址" type="text" class="layui-input"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">CODE</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入CODE" type="text" class="layui-input"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">平台</label>
            <div class="layui-input-block">
                <select name="sysId">
                    <option value="">-请选择-</option>
                    <option value="1">安卓</option>
                    <option value="2">苹果</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item model-form-footer">
            <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
            <button class="layui-btn" lay-filter="resource-form-submit" lay-submit>保存</button>
        </div>
    </form>
</script>


<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        table.render({
            elem: '#resource-table',
            url: config.base_server + 'api-select/resource',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {field: 'id', sort: true, title: 'ID'}
                ,{field: 'version', sort: true, title: '版本'}
                ,{field: 'code', sort: true, title: 'CODE'}
                ,{field: 'downUrl', sort: true, title: '下载地址'}
                ,{field: 'sysId', sort: true, title: '平台',templet:function(d){return d.sysId == 1 ? "安卓" : "苹果"}}
                ,{templet: '#resource-state', width: 120, align: 'center', title: '操作'}
            ]]
        });

        // 工具条点击事件
        table.on('tool(resource-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') {
                layer.confirm('确定删除吗？', function () {
                    layer.load(2);

                    admin.req('api-select/resource/del/'+obj.data.id, {  }, function (data) {
                        layer.closeAll('loading');
                        if (data.resp_code == 0) {
                            layer.msg(data.resp_msg, {icon: 1, time: 500});
                            table.reload('resource-table', {});
                        } else {
                            layer.msg(data.resp_msg, {icon: 2, time: 500});
                        }
                    }, 'DELETE');

                });
            }
        });

        // 添加按钮点击事件
        $('#resource-btn-add').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        var showEditModel = function (data) {
            layer.open({
                type: 1,
                title: data ? '修改' : '添加',
                area: '450px',
                offset: '120px',
                content: $('#resource-model').html(),
                success: function () {
                    $('#resource-form')[0].reset();
                    $('#resource-form').attr('method', 'POST');
                    if (data) {
                        form.val('resource-form', data);
                        $('#resource-form').attr('method', 'POST');
                    }
                }
            });
        };

        // 表单提交事件
        form.on('submit(resource-form-submit)', function (data) {
            layer.load(2);
            admin.req('api-select/resource/saveOrUpdate', JSON.stringify(data.field), function (data) {
                layer.closeAll('loading');
                if (data.resp_code == 0) {
                    layer.msg(data.resp_msg, {icon: 1, time: 500});
                    table.reload('resource-table');
                    layer.closeAll('page');
                } else {
                    layer.msg(data.resp_msg, {icon: 2, time: 500});
                }
            }, $('#resource-form').attr('method'));
            return false;
        });

        // 搜索按钮点击事件
        $('#resource-btn-search').click(function () {
            var key = $('#resource-search-key').val();
            var value = $('#resource-search-value').val();
            table.reload('resource-table', {where: {resourceKey: key, resourceValue: value}});
        });

    });

</script>